import $ from '../common';
Page({
  data: {
    titleHeight: $.gData().titleHeight,
    statuHeight: $.gData().statuHeight,
    defaultimg: '../../image/icon_defaule.png',
    activestateimg: '../../image/icon_selected.png', //手写的一个假的单选框的其中一个图片
    isdisplay: false, //是否显示底部全选框
    isIphoneX: $.gData().isIphoneX,
    isAllact: false, //是否全部选中
    actnum: 0, //选中多少个
    titlecontent: '选择学员点名状态', //模态框标题名称
    _components_modal_hide: true, //模态框默认不显示
    radiocontent: [{
      name: "到课",
      value: "normal"
    }, {
      name: "请假",
      value: "leave"
    }, {
      name: "缺课",
      value: "absent"
    }], //模态框的内容
    info: [],
    status: 'normal',
    student_uuids:[]
  },
  radioChange(tmp) { //记录模特框选择的状态
    this.setData({
      status: tmp.detail,
    })
  },
  determine() { //模态框的确定
  this.alterinfo((res=>{
    this.setData({
      _components_modal_hide: true
    })
  }))
  },
  alterinfo(ck){//修改状态并重新请求
    const { student_uuids,status,lesson_uuid} = this.data
    $.post('/teacher/rollcall', {
      status,
      student_uuids,
      lesson_uuid
    }, res => {
      if(ck){
        ck()
      }
      this.getinfo()
    })
  },
  handlitemclick(event) { //选择是单选的点击还是 全选的点击
    const {
      currentTarget: {
        dataset: {
          index,
          student_uuids
        }
      }
    } = event
    if (this.data.isdisplay) {
      this.switchimg(index)
    } else {
      this.itemclick(student_uuids)
    }
  },
  switchimg(index) { //批量点名时每一项的选中图片切换
    const { students } = this.data.info.lesson
    let uuidarr = []
    students.map((v, i) => {
      if (i == index) {
        v.isactive = !v.isactive
      }
      if (v.isactive) {
        uuidarr.push(v.student_uuid)
      }
    })
    let actnum = students.reduce((pre, current) => pre + (current.isactive ? 1 : 0), 0) //计算已勾选的数量
    if (actnum == students.length) {
      this.data.isAllact = true
    } else {
      this.data.isAllact = false
    }
    this.setData({
      'info.lesson.students': students,
      actnum: actnum,
      isAllact: this.data.isAllact,
      student_uuids:uuidarr
    })
  },
  itemclick(student_uuids) { //未批量点名的时候的点击事件模态框提示
    this.setData({
      _components_modal_hide: false,
      student_uuids: [student_uuids]
    })
  },
  batch() { //批量点名和单个点名的切换  批量点名的点击事件
    const {
      isdisplay
    } = this.data
    this.setData({
      isdisplay: !isdisplay
    })
  },
  checkboxChange() { //全选
    const { students } = this.data.info.lesson
    let { isAllact } = this.data
    isAllact = !isAllact
    let uuidarr = []

    students.map((v, i) => {
      v.isactive = isAllact
      if (v.isactive) {
        uuidarr.push(v.student_uuid)
      }
    })
    let actnum = students.reduce((pre, current) => pre + (current.isactive ? 1 : 0), 0) //计算已勾选的数量
    this.setData({
      'info.lesson.students': students,
      actnum: actnum,
      isAllact,
      student_uuids:uuidarr
    })
  },
  allnormal() { //全部到课按钮的点击
    this.setData({
      status:'normal'
    })
    this.alterinfo((res=>{
      this.setData({
        isdisplay:false,
        isAllact:false
      })
    }))
  },
  getinfo() {
    const {
      lesson_uuid
    } = this.data
    $.get('/teacher/lesson/detail', {
      lesson_uuid
    }, res => {
      res.lesson.students.forEach(v => {
        v.isactive = false
      });
      this.setData({
        info: res
      })
    })
  },
  onLoad(e) {
    const {
      lesson_uuid
    } = e
    this.setData({
      lesson_uuid
    })
    this.getinfo()
  },
  onShow() {}
})